const app = getApp()
Page({
  data: {
    newsDetail: {
      title: "",
      time: "",
      source: "",
      content: ""
    },
    currentPage: 1, // 当前页码
    totalPages: 1, // 总页数
    currentPageContent: [], // 当前页内容
	content:[],
    jumpPage: "", // 跳转页码
	
	  departmentList: [
	    
	  ],
  },

  onLoad(options) {
    const newsId = options.id;
    // 模拟从后台获取新闻详情数据
    const newsData = this.getNewsDetailById(newsId);
	console.log(newsId)
	console.log(newsData)
	// this.loadKeshiData()
    
  },
async loadKeshiData(init = false) {
	  var that = this
    try {
      // 模拟API请求
      app.util.request({
              url: 'Keshi/keshilists',
              data: {
              },
              success(res) {
                var data = res.data.data;
      			var list = data.keshi
				console.log(list)
				console.log(data)
                that.setData({
                    departmentList: list
                });
              }
      })
    } catch (error) {
      this.setData({ isLoading: false })
      wx.showToast({ title: '加载失败', icon: 'none' })
    }
  },
  
  	navigateToKeshiDetail:function(e){
  		
  		wx.navigateTo({
  		        url: '../keshi/detail?ksid='+e.currentTarget.dataset.id
  		})
  	},
  // 模拟获取新闻详情
  async getNewsDetailById(id) {
    var that = this
	// var newsDatabase = [];
	// var dataList = wx.getStorageSync('newlist')
	// console.log(dataList)
	// var detail = dataList.filter(function(i){
	// 	return i.id == id
	// })
	// console.log(detail)
	// console.log(detail[0])
	// var currentPageDetail = 1;
	// dataList.forEach(function(e,i){
	// 	if(e.id == id){
	// 		currentPageDetail = i+1
	// 	}
	// })
	 app.util.request({
	              url: 'Bjjy/detail',
	              data: {
					  'id':id
	              },
	              success(res) {
					console.log(res)
	                that.setData({
	                    newsDetail: res.data.data.bjjy,
	                    hospital: res.data.data.hospital,
	                    departmentList: res.data.data.bjjy.keshi,
						content:res.data.data.bjjy.content
	                });
	              }
	      })
	// that.setData({
	// 	newsDetail: detail[0],
	// 	departmentList:detail[0].keshi,
	// 	totalPages:dataList.length,
	// 	content: detail[0].content, // 明确设置 content
	// 	currentPage:currentPageDetail
	//   });
	
  },


	guahao:function(e){
		wx.navigateToMiniProgram({  
		      appId: e.currentTarget.dataset.appid, // 目标小程序的 AppID  
		      path: e.currentTarget.dataset.page, // 目标小程序内的页面路径，可带参数   
		      envVersion: 'release', // 打开正式版的小程序  
		      success(res) {  
		        // 跳转成功的回调  
		        console.log('跳转成功', res);  
		      },  
		      fail(err) {  
		        // 跳转失败的回调  
		        console.error('跳转失败', err);  
		      }  
		    });
	},
  // 上一页
  onPrevPage() {
    const { currentPage } = this.data;
    if (currentPage > 1) {
      this.setData({
		newsDetail: this.getPageContent(currentPage - 1),
        currentPage: currentPage - 1,
        content: this.getPageContent(currentPage - 1).content,		
		departmentList:this.getPageContent(currentPage - 1).keshi
      });
    }
  },

  // 下一页
  onNextPage() {
    const { currentPage, totalPages } = this.data;
    if (currentPage < totalPages) {
      this.setData({
		newsDetail: this.getPageContent(currentPage + 1),
        currentPage: currentPage + 1,
        content: this.getPageContent(currentPage + 1).content,		
		departmentList:this.getPageContent(currentPage + 1).keshi
      });
    }
  },

  // 获取指定页的内容
  getPageContent(pageIndex) {
	  var dataList = wx.getStorageSync('newlist')
	  console.log(dataList)
	  console.log(pageIndex)
	  console.log(dataList[pageIndex-1])
    return dataList[pageIndex-1];
  },
  // 跳转医院列表
  navigateToKeshiDetail(e) {
    const departmentId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/vp_pz/pages/bjjy/ksdetail?id=${departmentId}`
    })
  },
});